package de.koller.million.render;

import de.koller.oceanFramework.gl.util.Frame;
import de.koller.oceanFramework.gl.util.ParticleTexture;
import de.koller.oceanFramework.gl.util.ViewUtil;
import de.koller.oceanFramework.tree.Mother;
import de.koller.oceanFramework.tree.Sea3X;
import org.lwjgl.input.Keyboard;

public class Screen extends Sea3X {
	
	@Mother private RenderRoot rroot;
	
	private ParticleTexture lightLayer;
	private ParticleTexture screen;
	private boolean save = false;
	
	@Override
	protected void init() {
		super.init();
		
		lightLayer = ParticleTexture.generate( RenderStart.WIDTH, RenderStart.HEIGHT );
		screen = ParticleTexture.generate( RenderStart.WIDTH, RenderStart.HEIGHT );
	}

	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );

		if( rroot.input.isKeyPressed( Keyboard.KEY_RETURN ) )
			save = true;
	}

	@Override
	protected void render( int elapsedTime ) {
		super.render( elapsedTime );
		
		if( !save )
			return;
		
		ViewUtil.readPixels( screen );
		Frame.save( screen, "./output/screenshot.png" );
		System.out.println( "screenshot saved" );
		
		save = false;
	}

	void saveFrame( int frameIter ) {
		Frame.save( lightLayer, "./output/frame" + frameIter + ".png" );
	}

	public ParticleTexture getLightLayer() {
		return lightLayer;
	}
	
}
